package com.playball.lottery.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.playball.lottery.R;

public class LoadingDialog {

	Dialog dialog;
	TextView loading_tv_progress;
	ProgressBar loading_pb_bar;
	TextView loading_tv_tip;
	LinearLayout loading_ll_progress;
	
	int pb_width = 0;
	int ll_left = 0;
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
		}
	};
	
	public LoadingDialog(Context context) {
		dialog = createDialog(context);
	}

	private Dialog createDialog(Context context) {
		final Dialog loadingDialog = new Dialog(context, R.style.LoadingProgressDialog);
		
		loadingDialog.setCancelable(false);
		loadingDialog.setCanceledOnTouchOutside(false);

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.loading_dialog, null);
		
//		DisplayMetrics metrics=new DisplayMetrics();
//		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
//		int width=(int) ((metrics.widthPixels)*0.8);
		
//		Window dialogWindow = loadingDialog.getWindow();
//		WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
//		dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//		
//		layoutParams.width = width;
//		
//		dialogWindow.setAttributes(layoutParams);
		
		loadingDialog.setContentView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		
		loading_ll_progress = (LinearLayout) v.findViewById(R.id.loading_ll_progress);
		loading_tv_progress = (TextView) v.findViewById(R.id.loading_tv_progress);
		loading_pb_bar = (ProgressBar) v.findViewById(R.id.loading_pb_bar);
		loading_tv_tip = (TextView) v.findViewById(R.id.loading_tv_tip);
		
		loading_pb_bar.setMax(100);
		loading_pb_bar.setProgress(0);
		
		return loadingDialog;
	}
	
	public void show() {
		dialog.show();
	}
	
	public void cancel() {
		dialog.cancel();
	}
	
	public void setProgress(int progress) {
		loading_pb_bar.setProgress(progress);
		handler.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				loading_tv_progress.setText((loading_pb_bar.getProgress() * 100 / loading_pb_bar.getMax()) + "%");
				
                LayoutParams lp = (LayoutParams) loading_ll_progress.getLayoutParams();
                if(pb_width <= 0) {
                	pb_width =  loading_pb_bar.getWidth();
                }
                
                if(ll_left <= 0) {
                	ll_left = lp.leftMargin;
                }
                
                int pos = pb_width * (loading_pb_bar.getProgress() * 100 / loading_pb_bar.getMax()) / 100;
                lp.setMargins(ll_left + pos, lp.topMargin, lp.rightMargin, lp.bottomMargin);
                loading_ll_progress.setLayoutParams(lp);
			}
		});
	}
	
	public int getProgress() {
		return loading_pb_bar.getProgress();
	}
	
	public void setMax(int max) {
		if(max > 0)
			loading_pb_bar.setMax(max);
	}
	
	public int getMax() {
		return loading_pb_bar.getMax();
	}
	
	public Dialog getDialog() {
		return dialog;
	}
}
